﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;

namespace Hanel.DTN.News
{
    public class tblOS_Crawer_ChannelsManager : ItblOS_Crawer_ChannelsManager
    {
        private static ItblOS_Crawer_ChannelsManager itblOS_Crawer_ChannelsManager = null;

        private tblOS_Crawer_ChannelsManager()
        {
        }
        public static ItblOS_Crawer_ChannelsManager CreateInstance()
        {
            if (itblOS_Crawer_ChannelsManager == null)
            {
                itblOS_Crawer_ChannelsManager = new tblOS_Crawer_ChannelsManager();
            }
            return itblOS_Crawer_ChannelsManager;
        }

        public tblOS_Crawer_Channels SelectOS_Crawer_ChannelsByID(int id)
        {
            tblOS_Crawer_Channels tblOS_Crawer_Channels = new tblOS_Crawer_Channels();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
            parameters[0].Value = id;
            using (SqlConnection conn = new SqlConnection(DataHelper.GetConnectionString()))
            {
                IDataReader dr;
                SqlCommand sqlCommand = new SqlCommand("sp_SelectOS_Crawer_Channels", conn);
                sqlCommand.Parameters.AddRange(parameters);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                conn.Open();
                dr = sqlCommand.ExecuteReader();
                while (dr.Read())
                {
                    tblOS_Crawer_Channels.ID = Convert.ToInt32(dr["ID"].ToString());
                    tblOS_Crawer_Channels.ParentID = Convert.ToInt32(dr["ParentID"].ToString());
                    tblOS_Crawer_Channels.Name = (dr["Name"].ToString());
                    tblOS_Crawer_Channels.Description = (dr["Description"].ToString());
                    tblOS_Crawer_Channels.CreateDate = Convert.ToDateTime(dr["CreateDate"].ToString());
                   
                }
                dr.Close();
            }

            return tblOS_Crawer_Channels;
        }

        public DataTable SelectOS_Crawer_ChannelsDynamic(string Where, string Order)
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
            parameters[0].Value = Where;
            parameters[1].Value = Order;
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_SelectOS_Crawer_ChannelsDynamic", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }

        public DataTable SelectOS_Crawer_ChannelsDynamic_top(string Top, string Where, string Order)
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
                                                                new SqlParameter("@Top", SqlDbType.NVarChar,20),
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
            parameters[0].Value = Top;
            parameters[1].Value = Where;
            parameters[2].Value = Order;
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_SelectOS_Crawer_ChannelsDynamic_Top", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }

    }
}
